IF EXISTS (SELECT *
           FROM INFORMATION_SCHEMA.VIEWS
           WHERE TABLE_NAME='vBillDetail'
           AND TABLE_SCHEMA='dbo'
          )
          DROP VIEW dbo.vBillDetail
GO

CREATE VIEW dbo.vBillDetail
AS
/*@@*_____________________________________________________________________


_____________________________________________________________________*@@*/

  SELECT
    BillDetail.Id,
    BillDetail.ProductId,
    BillDetail.Amount,
    BillDetail.SalePrice,
    BillDetail.BillId,
    BillDetail.WarrantyTime,
    BillDetail.ProductPrice,
    TotalProductPrice = BillDetail.Amount * BillDetail.ProductPrice,
    TotalSalePrice = BillDetail.Amount * BillDetail.SalePrice,
    TotalInputPrice = BillDetail.Amount * ISNULL(BillDetail.InputPrice, 0),
    TotalDiscount = BillDetail.Amount * (BillDetail.ProductPrice - ISNULL(BillDetail.SalePrice, 0)),
    TotalBenefit = CASE 
						WHEN BillDetail.SalePrice < 0 THEN BillDetail.SalePrice
						ELSE (CASE 
								WHEN BillDetail.InputPrice > 0 THEN BillDetail.Amount *(BillDetail.SalePrice - BillDetail.InputPrice)
							ELSE 0
							END)
					END
  FROM
    BillDetail